package com.atguigu.gmall.cart.repository;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.common.util.JSONs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:lyd
 * @Date:2023/9/1 21:14
 * @Version:1.0
 * @Description
 **/
@Repository
public class CartRepository {

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 获取CartInfo信息
     * @param cartKey
     * @param skuId
     * @return
     */
    public CartInfo getCartInfo(String cartKey, Long skuId) {
        HashOperations<String, String, String> hash = redisTemplate.opsForHash();
        String jsonStr = hash.get(cartKey, skuId.toString());
        if (StringUtils.hasText(jsonStr)) {
            return  JSONs.jsonToObj(jsonStr, CartInfo.class);
        }
        return null;
    }

    /**
     * 保存 CartInfo数据
     * @param cartKey
     * @param cartInfo
     */
    public void saveCartInfo(String cartKey, CartInfo cartInfo) {
        HashOperations<String, String, String> hash = redisTemplate.opsForHash();
        //控制每一个商品数量的上限
        if (cartInfo.getSkuId() > RedisConst.CART_GOODS_ITEM_MAXNUM) {
            throw new GmallException(ResultCodeEnum.CART_GOODS_ITEM_MAXNUM);
        }
        hash.put(cartKey,cartInfo.getSkuId().toString(),JSONs.toJSONStr(cartInfo));
    }

    /**
     * 获取当前reids中cartKey下的所有数据集
     * @param cartKey
     * @return
     */
    public List<CartInfo> list(String cartKey) {
        HashOperations<String, String, String> hash = redisTemplate.opsForHash();
        List<CartInfo> cartInfos = hash.values(cartKey)
                .stream()
                .map(json -> {
                    return JSONs.jsonToObj(json, CartInfo.class);
                })
                .sorted((o1, o2) -> {return o2.getCreateTime().compareTo(o1.getCreateTime());})
                .collect(Collectors.toList());

        return cartInfos;
    }

    /**
     * 获取redis中所有商品的id
     * @param cartKey
     * @return
     */
    public List<Long> getSkuIdList(String cartKey) {
        HashOperations<String, String, String> hash = redisTemplate.opsForHash();
        //获取skuId
        List<Long> idList = hash.values(cartKey)
                .stream()
                .map(json -> JSONs.jsonToObj(json, CartInfo.class))
                .filter(item -> item.getIsChecked() == 1)//判断是否被选中
                .map(item -> item.getSkuId())
                .collect(Collectors.toList());
        if (idList != null && idList.size() > 0) {
            return idList;
        }
        return null;

    }

    /**
     * 删除零时购物车中指定skuId的商品
     * @param tempCartKey
     * @param skuId
     */
    public void deleteItem(String tempCartKey, Long skuId) {
        redisTemplate.opsForHash().delete(tempCartKey, skuId.toString());
    }

    /**
     * 给临时购物车设置过期时间
     * @param cartKey
     * @param ofDays
     */
    public void setTempCartTtl(String cartKey, Duration ofDays) {
        redisTemplate.expire(cartKey, ofDays);
    }

    /**
     * 判断购物车中容量的大小
     * @param cartKey
     * @return
     */
    public Long getCartSize(String cartKey) {
        return redisTemplate.opsForHash().size(cartKey);
    }

}
